home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils misc / Boxer 0.18 / Boxer018.exe / boxer / LoBrowz / lobrowz.c next >
Encoding:
C/C++ Source or Header  |  2000-07-08  |  4.5 KB  |  265 lines

  1. #define IGNORE_STDIO_STUBS
  2. #define __string_h
  3.  
  4. #include <PalmOS.h>
  5. #include <PalmCompatibility.h>
  6.  
  7. #include <Unix/sys_socket.h>
  8.  
  9. #include "stringil.h"
  10. #include "stdio2.h"
  11.  
  12. #ifdef __PALMOS_TRAPS__
  13. Err errno;
  14. #endif
  15.  
  16. char sendstr1[] = "GET /";
  17. char sendstr2[] = " HTTP/1.0\nHost: ";
  18. char sendstr3[] = "\nAccept: */*\n\n";
  19.  
  20. char buf[2050];
  21.  
  22. int MakeDatabase(FILE * fd);
  23.  
  24. int GetURL(char *host, char *path, int port)
  25. {
  26.   NetSocketRef sock = -1;       // socked file descriptor
  27.   FILE *fd;
  28.   char *cp = NULL;
  29.   Err err, err2;
  30.   Byte allup;
  31.   int i;
  32.   unsigned long len;
  33.   RectangleType r;
  34.  
  35.   AppNetRefnum = 0;
  36.  
  37.   err = SysLibFind("Net.lib", &AppNetRefnum);
  38.   err = NetLibOpen(AppNetRefnum, &err2);
  39.   NetLibConnectionRefresh(AppNetRefnum, true, &allup, &err2);
  40.  
  41.   sock = NetUTCPOpen(host, NULL, port);
  42.  
  43.   if (sock < 0)
  44.     return 1;
  45.  
  46.   AppNetTimeout = -1;
  47.  
  48.   WinDrawChars("Connect   ", 10, 0, 112);
  49.  
  50.   send(sock, sendstr1, strlen(sendstr1), 0);
  51.   send(sock, path, strlen(path), 0);
  52.   send(sock, sendstr2, strlen(sendstr2), 0);
  53.   send(sock, host, strlen(host), 0);
  54.   send(sock, sendstr3, strlen(sendstr3), 0);
  55.  
  56.   len = 0;
  57.  
  58.   for (;;) {
  59.  
  60.     WinDrawChars("Wait      ", 10, 0, 112);
  61.     i = recv(sock, &buf[len], 2048 - len, 0);
  62.  
  63.     if (i < 0)
  64.       break;
  65.  
  66.     if (!i) {
  67.       WinDrawChars("Bad Header         ", 20, 0, 112);
  68.       SysTaskDelay(100);
  69.       break;
  70.     }
  71.  
  72.     len += i;
  73.  
  74.     buf[len] = 0;
  75.  
  76.     cp = buf;
  77.  
  78.     while (*cp && strncmp(cp, "\r\n\r\n", 4))
  79.       cp++;
  80.     if (*cp) {
  81.       WinDrawChars(buf, 80, 0, 100);
  82.       cp += 4;
  83.       break;
  84.     }
  85.  
  86.     WinDrawChars("NoEOH     ", 10, 0, 112);
  87.  
  88.     SysTaskDelay(100);
  89.   }
  90.  
  91.   WinDrawChars(buf, 32, 40, 112);
  92.  
  93.   if (i <= 0) {
  94.     close(sock);
  95.     return 1;
  96.   }
  97.  
  98.   r.topLeft.x = 0, r.topLeft.y = 0, r.extent.x = 160, r.extent.y = 15;
  99.  
  100.   WinDrawChars("Open      ", 10, 0, 112);
  101.  
  102.   WinEraseRectangle(&r, 0);
  103.  
  104.   fd = FileOpen(0, "WWWFileTempXYZ", 'DATA', 'BRWS', fileModeReadWrite, NULL);
  105.  
  106.   len -= (cp - buf);
  107.  
  108.   fwrite(cp, 1, len, fd);
  109.  
  110.   cp = buf;
  111.   while (*cp && strncmp(cp, "Content-Length: ", 16))
  112.     cp++;
  113.   if (*cp) {
  114.     char *dp;
  115.     cp += 16;
  116.     dp = cp;
  117.     while (*dp >= ' ')
  118.       dp++;
  119.     *dp = 0;
  120.     WinDrawChars(cp, strlen(cp), 0, 100);
  121.   }
  122.  
  123.   for (;;) {
  124.  
  125.     i = recv(sock, buf, 1024, 0);
  126.  
  127.     if (!i) {
  128.       WinDrawChars("Stall      ", 10, 0, 112);
  129.       AppNetTimeout = 1000;
  130.       i = recv(sock, buf, 1024, 0);
  131.       AppNetTimeout = -1;
  132.       if (!i)
  133.         break;
  134.     }
  135.  
  136.     if (i <= 0)
  137.       break;
  138.     len += i;
  139.     WinDrawChars("More      ", 10, 0, 112);
  140.     if (i != fwrite(buf, 1, i, fd)) {
  141.       WinDrawChars("WERR      ", 10, 0, 112);
  142.       fclose(fd);
  143.       unlink("WWWFileTempXYZ");
  144.       close(sock);
  145.       SysTaskDelay(300);
  146.       return 3;
  147.     }
  148.  
  149.     StrIToA(buf, len);
  150.     strcat(buf, "    ");
  151.     WinDrawChars(buf, strlen(buf), 0, 100);
  152.  
  153.   }
  154.  
  155.   close(sock);
  156.  
  157.   fclose(fd);
  158.   rename("WWWFileTempXYZ", path);
  159.  
  160.   return 0;
  161. }
  162.  
  163.  
  164. void BreakGetURL(char *tp1)
  165. {
  166.  
  167.   unsigned char *tp2, *tp3;
  168.   unsigned int port = 80, x = 0;
  169.  
  170.  
  171.   tp2 = NULL;
  172.  
  173.   /* break apart URL */
  174.  
  175.   tp3 = tp1;
  176.   while (*tp3 && *tp3 != '/' && *tp3 != ':')
  177.     tp3++;
  178.  
  179.   if (*tp3 == ':') {
  180.     *tp3++ = 0;
  181.     tp2 = tp3;
  182.     while (*tp3 && *tp3 != '/' && *tp3 != ':')
  183.       tp3++;
  184.   }
  185.  
  186.   x = 0;
  187.   if (tp2 && strlen(tp2))
  188.     x = StrAToI(tp2);
  189.   if (x)
  190.     port = x;
  191.  
  192.   GetURL(tp1, tp3, port);
  193. }
  194.  
  195. FormPtr form = NULL;
  196.  
  197. Boolean handleit(EventPtr event)
  198. {
  199.   FieldPtr tf1;
  200.   unsigned char *tp1;
  201.   unsigned int eid;
  202.  
  203.   switch (event->eType) {
  204.  
  205.   case frmOpenEvent:
  206.     FrmDrawForm(form);
  207.     FrmSetFocus(form, FrmGetObjectIndex(form, 1001));
  208.     return 1;
  209.  
  210.   case ctlSelectEvent:
  211.  
  212.     eid = event->data.ctlEnter.controlID;
  213.     /* process form */
  214.     if (eid == 1005) {
  215.  
  216.       tf1 = FrmGetObjectPtr(form, FrmGetObjectIndex(form, 1001));
  217.       tp1 = FldGetTextPtr(tf1);
  218.  
  219.       BreakGetURL(tp1);
  220.       WinEraseWindow();
  221.       FrmDrawForm(form);
  222.     }
  223.     return 1;
  224.   default:
  225.     return 0;
  226.   }
  227.   return 0;
  228. }
  229.  
  230.  
  231. DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
  232. {
  233.   EventType event;
  234.   Err err;
  235.   int fid;
  236.  
  237.   if (cmd != sysAppLaunchCmdNormalLaunch)
  238.     return 0;
  239.  
  240.     FrmGotoForm(1000);
  241.  
  242.     do {
  243.       EvtGetEvent(&event, -1);
  244.  
  245.       if (SysHandleEvent(&event))
  246.         continue;
  247.       if (MenuHandleEvent((void *) 0, &event, &err))
  248.         continue;
  249.  
  250.       if (event.eType == frmLoadEvent) {
  251.         fid = event.data.frmLoad.formID;
  252.         form = FrmInitForm(fid);
  253.         FrmSetActiveForm(form);
  254.         FrmSetEventHandler(form, (FormEventHandlerPtr) handleit);
  255.       }
  256.  
  257.       if (form)
  258.         FrmDispatchEvent(&event);
  259.  
  260.     }
  261.     while (event.eType != appStopEvent);
  262.  
  263.   return 0;
  264. }
  265.